package org.darkforest.common.services.impl;

import org.darkforest.common.services.GoodsClassRedisService;
import org.darkforest.dao.mapper.GoodsClassMapper;
import org.darkforest.dao.mapper.PBoothGoodsMapper;
import org.darkforest.dao.redis.GoodsClassRedisDao;
import org.darkforest.model.GoodsClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by valentina on 15/2/19.
 * 商品分类列表在redis中的存储/读取操作
 */
@Service
public class GoodsClassRedisServicesImpl implements GoodsClassRedisService {
    Logger logger = LoggerFactory.getLogger(GoodsClassRedisServicesImpl.class);

    @Autowired
    private GoodsClassMapper goodsClassMapper;

    @Autowired
    private GoodsClassRedisDao goodsClassRedisDao;

    @Autowired
    private PBoothGoodsMapper pBoothGoodsMapper;

    @Override
//    @PostConstruct
    public void initializedClassList() {
        logger.debug("=======initialized goods class begin=====");
        logger.debug("gc_list:" + goodsClassRedisDao.getGoodsClassList("gc_list"));
        //初始化时赋值
        if (goodsClassRedisDao.getGoodsClassList("gc_list").size() == 0) {
            GoodsClass goodsClassParam = new GoodsClass();
            goodsClassParam.setGcParentId(0);
            goodsClassParam.setPageSwitch(false);
            List<GoodsClass> parentClass = goodsClassMapper.findGoodsClass(goodsClassParam);
            if (parentClass != null) {
                goodsClassRedisDao.addClassList("gc_list", parentClass);
                logger.debug("----add gc_list----");
                for (GoodsClass cls : parentClass) {
                    logger.debug("----gc_list:" + cls.getGcId() + ",name:" + cls.getGcName() + "-----");
                    goodsClassParam.setGcParentId(cls.getGcId());
                    List<GoodsClass> level2Class = goodsClassMapper.findGoodsClass(goodsClassParam);
                    if (level2Class != null) {
                        goodsClassRedisDao.addClassList("gc_list_" + cls.getGcId(), level2Class);

                        for (GoodsClass subCls : level2Class) {
                            logger.debug("----sub gc_list:" + subCls.getGcId() + ",sub cls name:" + subCls.getGcName() + "-----");
                            goodsClassParam.setGcParentId(subCls.getGcId());
                            List<GoodsClass> level3Class = goodsClassMapper.findGoodsClass(goodsClassParam);
                            if (level3Class != null) {
                                goodsClassRedisDao.addClassList("gc_list_" + subCls.getGcId(), level3Class);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void removeAllClassList() {

    }

    /**
     * 从redis中获取分类列表
     *
     * @param parentClsId ：可为空，为空时取一级分类列表
     * @return
     */
    @Override
    public List<GoodsClass> getGoodsClassList(Integer parentClsId) {
        String key = "gc_list";
        if (parentClsId != null) {
            key += "_" + parentClsId;
        }
        return goodsClassRedisDao.getGoodsClassList(key);
    }

    /**
     * 获取所有二级商品分类，页面显示使用
     * @return
     */
    @Override
    public List<List<GoodsClass>> getAllSubList() {
        List<List<GoodsClass>> res = new ArrayList<List<GoodsClass>>();
        List<GoodsClass> parents = this.getGoodsClassList(null);
        for(GoodsClass cls : parents){
            res.add(this.getGoodsClassList(cls.getGcId()));
        }
        return res;
    }

    @Override
    public void updateGoodsClassList(Integer parentClsId) {
        GoodsClass goodsClassParam = new GoodsClass();
        goodsClassParam.setGcParentId(parentClsId);
        List<GoodsClass> list = goodsClassMapper.findGoodsClass(goodsClassParam);
        goodsClassRedisDao.deleteList("gc_list_"+parentClsId);
        goodsClassRedisDao.addClassList("gc_list_"+parentClsId,list);
    }

    @Override
    public List<GoodsClass> getGoodsClassListForPC() {
        List<GoodsClass> gcs = goodsClassRedisDao.getGoodsClassList("gc_list_index");
        if(gcs == null || gcs.size() == 0){
            gcs=pBoothGoodsMapper.selectGoodsClassIds();
            goodsClassRedisDao.addClassList("gc_list_index",gcs);
        }
        return gcs;
    }
}
